package com.example.sunmiv1.adapter;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.example.sunmiv1.R;
import com.example.sunmiv1.entity.GoodBean;

import java.util.List;
import java.util.Objects;

import butterknife.BindView;
import butterknife.ButterKnife;

public class SkuKindAdapter extends BaseAdapter {
    private int grid_position;
    private LayoutInflater inflater;
    private List<GoodBean.DataBean.GoodsBean.SkuAttrBean.ListBean> detailList;

    public SkuKindAdapter(Context context, List<GoodBean.DataBean.GoodsBean.SkuAttrBean.ListBean> detailList) {
        inflater = LayoutInflater.from(context);
        this.detailList = detailList;
    }

    @Override
    public int getCount() {
        return detailList.size();
    }

    @Override
    public Object getItem(int i) {
        return detailList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder vh;
        if (view == null) {
            view = inflater.inflate(R.layout.specification_kind_item, null);
            vh = new ViewHolder(view);
            view.setTag(vh);
        } else {
            vh = (ViewHolder) view.getTag();
        }
        vh.skuKindName.setText(detailList.get(i).getName());
        if (grid_position == i) {
            vh.skuKindName.setBackgroundResource(R.drawable.sku_xbg);
            vh.skuKindName.setTextColor(Color.parseColor("#FFFFFF"));
        } else {
            vh.skuKindName.setBackgroundResource(R.drawable.sku_bg);
            vh.skuKindName.setTextColor(Color.parseColor("#666666"));
        }
        return view;
    }

    static class ViewHolder {
        @BindView(R.id.sku_kind_name)
        TextView skuKindName;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }

    public void getPosition(int position) {
        this.grid_position = position;
        notifyDataSetChanged();
    }
    public int getPositionId(String select) {
        int index=-1;
        for (int i = 0; i < detailList.size(); i++) {
            if(Objects.equals(detailList.get(i).getPath(), select)){
                grid_position=i;
                index=i;
            }
        }
        notifyDataSetChanged();
        return index;
    }
}
